/**
 * @file    loginwindow.cpp
 * @author  Siddharth Sathyam <ssathy2@gmail.com>
 * @author  Alan Ruvalcaba <aruval3@gmail.com>
 * @author  Pat LaMontagna <plamontagna@gmail.com>
 * @version 1.0
 * @section LICENSE
 *
 * This program is property of Siddharth Sathyam, Alan Ruvalcaba, Pat LaMontagna.
 *
 * @section DESCRIPTION
 *
 * The loginwindow is the way that the user logs in to the server.
 */

#include "loginwindow.h"
#include "ui_loginwindow.h"
#include "QMessageBox"
#include "QWidget"

LoginWindow::LoginWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LoginWindow)

{
    ui->setupUi(this);


    // Hardcode the QuitButton attributes
    ui->QuitButton->setIcon(QIcon(":icons/exit.png"));
    ui->QuitButton->setIconSize(QSize (51,41));
    ui->QuitButton->setFlat(true);

    // Hardcode the AboutButton attributes
    ui->AboutButton->setIcon(QIcon(":icons/about.png"));
    ui->AboutButton->setIconSize(QSize (51,41));
    ui->AboutButton->setFlat(true);

    // Hardcode the AddBuddy attributes
    ui->LoginButton->setIcon(QIcon(":icons/send.png"));
    ui->LoginButton->setIconSize(QSize (51,41));
    ui->LoginButton->setFlat(true);

    this->installEventFilter(this);
    ui->nameBox->setFocus();
}

LoginWindow::~LoginWindow()
{
    delete ui;
}

void LoginWindow::on_LoginButton_clicked()
{
    QString name = ui->nameBox->text().trimmed();

    if(name.isEmpty())
    {
	return;
    }

    QString serverAddress = ui->serverAddressEditBox->text();
    quint16 serverPort = ui->serverPortEditBox->text().toInt();

    loginSocket = new Client(serverAddress, serverPort , name);

    if(!loginSocket->startClient())
    {
	this->show();
    }
    else
    {
	ContactList *contacts = new ContactList(0,loginSocket);
	contacts->show();
	this->close();
    }
}

void LoginWindow::on_AboutButton_clicked()
{
    QMessageBox *about = new QMessageBox(0);
    about->setWindowTitle("About");
    about->setInformativeText("N3RD CH4T Developed By: \n ---------------------\n Alan Ruvalcaba \n Siddharth Sathyam \n Patrick LaMontagna \n Free Icon Set (GPL License) by: Alessandro Rei");
    about->exec();
}

void LoginWindow::on_QuitButton_clicked()
{
    this->close();
}
void LoginWindow::on_nameBox_textEdited(const QString &arg1)
{
    this->name = arg1;
}

bool LoginWindow::eventFilter(QObject *ob, QEvent *e)
{
    QKeyEvent *keyEvent = static_cast<QKeyEvent*>(e);

    //Check if object requesting an event is the name box for login
    if(ob == this)
    {
	//Check for return key to have been pressed
	if(keyEvent->key() == Qt::Key_Return && e->type() == QEvent::KeyPress)
	{
	    //Check if we dont have an empty login name
	    if(!ui->nameBox->text().trimmed().isEmpty())
	    {

		QString serverAddress = ui->serverAddressEditBox->text();
		quint16 serverPort = ui->serverPortEditBox->text().toInt();

		loginSocket = new Client(serverAddress, serverPort , ui->nameBox->text());

		//Try to connect to host
		if(!loginSocket->startClient())
		{
		    this->show();
		    return false;
		}
		else
		{
		    //Create contact list and close login window
		    ContactList *contacts = new ContactList(0 , loginSocket);
		    contacts->show();
		    this->close();
		    return true;
		}
	    }
	}
    }

    return false;
}

